
$(document).ready(function() {
        //查询方法
        function QueryList(){
        }
        //查询方法的原型
        var queryListProto = {
            queryByStr: function(v_isBlack ,v_merchantName){
                let obj ={
                		"isBlack":v_isBlack,
                		"merchantName":v_merchantName
                };
            $.ajax({
	    		url: "./MerchantListServlet",
	    		type:"post", 
	    		dataType:"json", 
	    		data:obj,
	    		success:function(result) { 
                    
	    			if(result.status === 'success'){
                        let list = result.data;
                         let content = "";
                         $("tr:not(.active)").remove();
                         $("#nullDataMsg").remove();
                         $.each(list, function(i,item){    
                             content = content +`<tr>
								<td>${item.merchantName }</td>
								<td>${item.merchantAddress }</td>
								<td><img src="${item.merchantPic }"
									style="width: 80px; height: 80px" /></td>
								<td><img src="${item.merchantIdcard } "
									style="width: 120px; height: 80px" /></td>
								<td>` ;
                                if(item.auditStatus == 0){
                                    content = content + `审核通过`;
                                }else if(item.auditStatus == 1){
                                      content = content + `审核驳回`;
                                }else if(item.auditStatus == 2){
                                      content = content + `待审核`;
                                }
                                  content = content +`</td>
								<td>${item.remarks ===null ? "" : item.remarks }</td>

								<td class="option-btn "><input type="text" value="${item.lastApplyTime}" hidden="true" name="lastApplyTime"/>`;
                                if(item.auditStatus == 1 || item.auditStatus == 2){
                                    content = content + `<button class="btn btn-success " onclick="passApply(this)" value="${item.merchantId}">确认通过</button>
										<button class="btn btn-danger" onclick="rejectApply(this)" value="${item.merchantId}">驳回</button>`;
                                }
                                if(item.isBlack == 0){
                                      content = content + `<button class="btn btn-info " value="${item.merchantId}" onclick="addInBlack(this)" >加入黑名单</button>
										<button class="btn btn-primary  " value="${item.merchantId}"  style="display:none" onclick="addInWhite(this)">加入白名单</button>`;
                                }else if(item.isBlack == 1){
                                      content = content + `<button class="btn btn-info " value="${item.merchantId}"  style="display:none" onclick="addInBlack(this)" >加入黑名单</button>
										<button class="btn btn-primary  " value="${item.merchantId}" onclick="addInWhite(this)" >加入白名单</button>`;
                                }
								  content = content +`</td></tr>`;
                            
                         });
                         $("tbody").append(content);
	    			}else{
	    				 $("tr:not(.active)").remove();
                        content = `<h2 align="center" id="nullDataMsg">${result.msg}</h2>`;
                       $("table").after(content);
                    }
	    			
			     },  
			     error : function() {  
			           alert("异常！");  
			           $('#myModal').modal('hide');
			      }  
	    	});
            }
        }
      QueryList.prototype = queryListProto;
    
    //第一次进入页面加载商家列表
    var queryList = new QueryList();
    queryList.queryByStr("","");
    
    //通过申请、拉黑、拉白
	$('#myModalSubmit').on('click',function(){
		var requestUrl = $('#requestUrl').val();
		var merchantId = $('#merchantId').val();
        var lastApplyTime =  $('#lastApplyTime').val();
		var type= $('#type').val();
		var isBlack = $('#isBlack').val();
		var obj = {  
	               "merchantId" : merchantId,
	               "type" :type,
	               "isBlack" :isBlack,
                    "lastApplyTime" :lastApplyTime
	     };
	    	$.ajax({
	    		url: requestUrl,
	    		type:"post", 
	    		dataType:"json", 
	    		data:obj,
	    		success:function(data) { 
                    $('#myModal').modal('hide');
	    			if(data.status === 'success'){
                        $("#alertDiv span").empty();
	    				$("#alertDiv span").prepend("修改成功！&times;");
	    				$("#alertDiv").attr("style","display:''");
			             $("#alertDiv").alert();
	    				if(type=="isBlack" && isBlack==="1"){
	    					$(".option-btn .btn-primary[value='"+merchantId+"']").attr("style","display:''");
	    					$(".option-btn .btn-info[value='"+merchantId+"']").attr("style","display:none");
	    				}else if(type=="isBlack" && isBlack==="0"){
	    					$(".option-btn .btn-info[value='"+merchantId+"']").attr("style","display:''");
	    					$(".option-btn .btn-primary[value='"+merchantId+"']").attr("style","display:none");
	    				}else if(type=="apply"){
	    					$(".option-btn .btn-success[value='"+merchantId+"']").parent().parent().children("td").eq(4).text("申请通过");
	    					$(".option-btn .btn-success[value='"+merchantId+"']").remove();
	    					$(".option-btn .btn-danger[value='"+merchantId+"']").remove();
	    				}
	    				
	    			}else{
                        $("#failAlertDiv span").empty();
                        $("#failAlertDiv span").prepend(data.msg+"&times;");
                         
                        $("#failAlertDiv").attr("style","display:''");
			             $("#failAlertDiv").alert();
                    }
	    			
			     },  
			     error : function() {  
			           alert("异常！");  
			           $('#myModal').modal('hide');
			      }  
	    	});
	});
    //驳回提交
	$('#rejectModalSubmit').on('click',function(){
		var remarks = $('#remarks').val();
		if(remarks != ""){
			var requestUrl = $('#requestUrl').val();
			var merchantId = $('#merchantId').val();
			var obj = {  
		               "merchantId" : merchantId,
		               "type" :"reject",
		               "remarks" :remarks
		     };
		    	$.ajax({
		    		url: requestUrl,
		    		type:"post", 
		    		dataType:"json", 
		    		data:obj,
		    		success:function(data) { 
	                    $('#rejectModal').modal('hide');
		    			if(data.status === 'success'){
                             $("#alertDiv span").empty();
		    				$("#alertDiv span").prepend("修改成功！&times;");
		    				$("#alertDiv").attr("style","display:''");
			                 $("#alertDiv").alert();
		    				$(".option-btn .btn-danger[value='"+merchantId+"']").parent().parent().children("td").eq(5).text(remarks);
		    				
		    			}else{
                             $("#failAlertDiv span").empty();
	                        $("#failAlertDiv span").prepend(data.msg+"&times;");
	                        $("#failAlertDiv").attr("style","display:''");
			                 $("#failAlertDiv").alert();
	                    }
		    			
				     },  
				     error : function() {  
				           alert("异常！");  
				           $('#myModal').modal('hide');
				      }  
		    	});
		}else{
			$('#rejectModal').modal('hide');
			 $("#failAlertDiv span").empty();
			 $("#failAlertDiv span").prepend("驳回理由不能为空！&times;");
             $("#failAlertDiv").attr("style","display:''");
			 $("#failAlertDiv").alert();
		}
		
		
	});
	$('#failAlertDiv').on('close.bs.alert', function () {
		  let a = `<div class="alert alert-danger   fade  in" style="display: none" id="failAlertDiv">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			  <span aria-hidden="true" > &times;</span>
			</button>
		</div>`;
		 $(".panel.panel-info.table-responsive .panel-body").append(a);
	})
	$('#alertDiv').on('close.bs.alert', function () {
		  let a = `<div class="alert alert-success   fade  in" style="display: none" id="alertDiv">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close">
			  <span aria-hidden="true" > &times;</span>
			</button>
		</div>`;
		 $(".panel.panel-info.table-responsive .panel-body ").append(a);
	})
    //根据拉黑状态搜索
    $(".dropdown-menu a").on('click',function(){
        let txt = $(this).text();
        $(".dropdown .btn.btn-default.dropdown-toggle").html(txt +` <span class="caret"></span>`);
        let isblack = $(this).attr("value");
        let v_name = $("#seachNameInput").val();
        var queryList = new QueryList();
    
        queryList.queryByStr(isblack,v_name);
    });
	//根据商家名字搜索
	$("#seachByNameBtn").on('click',function(){
        let isblack = $(this).attr("value");
	    let v_name = $("#seachNameInput").val();
	    var queryList = new QueryList();
	    queryList.queryByStr(isblack,v_name);
	});
});

//通过审核
function passApply(item){
  let merchantId =item.value;
  console.log($(item).parent("tr"));
  let lastApplyTime = $(item).prev().val();
  console.log(lastApplyTime);
    $('#myModal').modal('toggle');
    $('#myModalLabel').html('审核');
    $('#modal-msg p').html("你确定审核通该商家的申请吗？");

    $('#merchantId').val(merchantId);
    $('#type').val("apply");
    $('#requestUrl').val("./MerchantApplyServlet");
    $('#lastApplyTime').val(lastApplyTime);
}
 //驳回申请	
function rejectApply(item){
  let merchantId = item.value;
  let lastApplyTime = $(item).prev().val();
		$('#rejectModal').modal('toggle');
		$('#remarks').val("");
		$('#rejectModalLabel').html('请填写驳回理由');
		
		$('#merchantId').val(merchantId);
		$('#type').val("reject");
		$('#requestUrl').val("./MerchantApplyServlet");
     $('#lastApplyTime').val(lastApplyTime);
}
//拉白商家
function addInWhite(item){
  let merchantId = item.value;
	$('#myModal').modal('toggle');
		$('#myModalLabel').html('拉白');
		$('#modal-msg p').html("你确定拉白该商家吗？");
		
		$('#merchantId').val(merchantId);
		$('#type').val("isBlack");
		$('#isBlack').val("0");
		$('#requestUrl').val("./MerchantApplyServlet");
}
//拉黑商家
function addInBlack(item){
  let merchantId = item.value;
		$('#myModal').modal('toggle');
		$('#myModalLabel').html('拉黑');
		$('#modal-msg p').html("你确定拉黑该商家吗？");
		
		$('#merchantId').val(merchantId);
		$('#type').val("isBlack");
		$('#isBlack').val("1");
		$('#requestUrl').val("./MerchantApplyServlet");
}

